package com.anthonyfdev.dropdownviewexample.slice;

import com.anthonyfdev.dropdownview.DropDownView;
import com.anthonyfdev.dropdownviewexample.DropDownAdapter;
import com.anthonyfdev.dropdownviewexample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.service.WindowManager;

public class DropDownAbilitySlice extends AbilitySlice {

    public static final int NUM_OF_STANDS = 14;
    private ListContainer recyclerView;
    private DropDownAdapter adapter;
    private int selectedStandId;
    private String[] waitTimes;
    private Text selectedStandTitleTV;
    private Text selectedStandStatusTV;
    private Component collapsedView;
    private DropDownView dropDownView;
    private Component expandedView;
    private Image headerChevronIV;
    private int ANGLE = 180;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_drop_down);

        WindowManager.getInstance().getTopWindow().get()
                .setStatusBarColor(getColor(ResourceTable.Color_colorPrimaryDark));

        setupViews();
        setupList();

        dropDownView.setHeaderView(collapsedView);
        dropDownView.setExpandedView(expandedView);
        dropDownView.setExpandedViewHeight(AttrHelper.vp2px(1920, getContext()));
        dropDownView.setDropDownListener(dropDownListener);
    }

    @Override
    protected void onActive() {
        super.onActive();
        headerChevronIV.setRotation(dropDownView.isExpanded() ? 180f : 0f);
    }

    public void setStandStateWithId(String waitTime, int standId) {
        if (standId >= 0 && standId < NUM_OF_STANDS) {
            waitTimes[standId] = waitTime;
            adapter.notifyDataSetItemChanged(standId);
        }

        // Should update currently selected stand wait time as well
        if (selectedStandId == standId) {
            selectedStandStatusTV.setText(waitTime);
        }
    }

    private void setupList() {
        waitTimes = new String[] {"3 minute wait", "Closed", "No wait time", "10 minute wait", "10 minute wait", "10 minute wait", "10 minute wait", "10 minute wait", "10 minute wait", "10 minute wait", "10 minute wait", "10 minute wait", "10 minute wait", "10 minute wait"};
        viewActions.setSelectedStand(1);
        adapter = new DropDownAdapter(viewActions);
        recyclerView.setItemProvider(adapter);
        for (int i = 0; i < waitTimes.length; i++) {
            setStandStateWithId(waitTimes[i], i);
        }
    }

    private void setupViews() {
        dropDownView = (DropDownView) findComponentById(ResourceTable.Id_drop_down_view);
        collapsedView = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_view_my_drop_down_header, null, false);
        expandedView = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_view_my_drop_down_expanded, null, false);

        selectedStandTitleTV = (Text) collapsedView.findComponentById(ResourceTable.Id_selected_stand_title);
        selectedStandStatusTV = (Text) collapsedView.findComponentById(ResourceTable.Id_selected_stand_status);
        recyclerView = (ListContainer) expandedView.findComponentById(ResourceTable.Id_recyclerView);
        headerChevronIV = (Image) collapsedView.findComponentById(ResourceTable.Id_chevron_image);
    }

    private final DropDownView.DropDownListener dropDownListener = new DropDownView.DropDownListener() {
        @Override
        public void onExpandDropDown() {
            adapter.notifyDataChanged();
            headerChevronIV.createAnimatorProperty().rotate(ANGLE).start();
            ANGLE += 180;
        }

        @Override
        public void onCollapseDropDown() {
            headerChevronIV.createAnimatorProperty().rotate(ANGLE).start();
            ANGLE += 180;
        }
    };

    private final DropDownAdapter.ViewActions viewActions = new DropDownAdapter.ViewActions() {
        @Override
        public void collapseDropDown() {
            dropDownView.collapseDropDown();
        }

        @Override
        public void setSelectedStand(int standId) {
            selectedStandTitleTV.setText(getStandTitle(standId));
            selectedStandStatusTV.setText(getStandStatus(standId));
            selectedStandId = standId;
        }

        @Override
        public String getStandTitle(int standId) {
            String title = "";
            switch (standId) {
                case 0:
                    title = "Brooklyn Lemonade";
                    break;
                case 1:
                    title = "Manhattan Bourjee Sliders";
                    break;
                case 2:
                    title = "Queens & Cakes";
                    break;
                case 3:
                    title = "Bronx Tea";
                    break;
                default:
                    title = "Bronx Tea " + standId;
                    break;
            }
            return title;
        }

        @Override
        public String getStandStatus(int standId) {
            return waitTimes[standId] != null ? waitTimes[standId] : "";
        }

        @Override
        public int getSelectedStand() {
            return selectedStandId;
        }
    };
}
